package com.hejian.android.a72ggametools.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.hejian.android.a72ggametools.R;
import com.hejian.android.a72ggametools.activity.PrizeDetailActivity;
import com.hejian.android.a72ggametools.adapter.ConvertAdapter;
import com.hejian.android.a72ggametools.adapter.ConvertPageAdapter;
import com.hejian.android.a72ggametools.bean.Convert;
import com.hejian.android.a72ggametools.common.Constant;
import com.hejian.android.a72ggametools.net.HttpUtils;
import com.hejian.android.a72ggametools.utils.JsonParse;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/1/13.
 */
public class ConvertFragment extends Fragment implements View.OnClickListener {
    private View view;
    private Button newPrizeBtn;
    private Button hotPrizeBtn;
    private ViewPager scoreViewPager;
    private View view1;
    private View view2;
    private ListView newPrizeListView;
    private ListView hotPrizeListView;
    private ArrayList<View> viewList;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_convert, null);
        initView();
        initData();
        setAdatper();
        setListener();

        return view;
    }

    private void setListener() {
        setViewPagerListener();
        setBtnListener();
        setListListener();
    }

    private void setListListener() {
        newPrizeListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(),PrizeDetailActivity.class);
                intent.putExtra("id",list1.get(position).getId());
                startActivity(intent);
            }
        });
        hotPrizeListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(),PrizeDetailActivity.class);
                intent.putExtra("id",list2.get(position).getId());
                startActivity(intent);
            }
        });
    }

    private void setBtnListener() {
        newPrizeBtn.setOnClickListener(this);
        hotPrizeBtn.setOnClickListener(this);
    }

    private void setViewPagerListener() {
        scoreViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                setCurrentBtn(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void setCurrentBtn(int position) {
        switch (position){
            case 0:
                newPrizeBtn.setBackgroundResource(R.drawable.half_left_shape_blue);
                newPrizeBtn.setTextColor(Color.WHITE);
                hotPrizeBtn.setBackgroundResource(R.drawable.half_right_shape_white);
                hotPrizeBtn.setTextColor(Color.BLUE);
                break;
            case 1:
                newPrizeBtn.setBackgroundResource(R.drawable.half_left_shape_white);
                newPrizeBtn.setTextColor(Color.BLUE);
                hotPrizeBtn.setBackgroundResource(R.drawable.half_right_shape_blue);
                hotPrizeBtn.setTextColor(Color.WHITE);
                break;
        }
    }

    private void setAdatper() {
        ConvertPageAdapter convertPageAdapter = new ConvertPageAdapter(viewList);
        scoreViewPager.setAdapter(convertPageAdapter);

    }

    private void initData() {
        loadNextNewPage();
        loadNextHotPage();
    }


    private void loadNextHotPage() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                loadHotPage();
            }


        }).start();
    }

    private void loadNextNewPage() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                loadNewPage();
            }
        }).start();
    }

    private void loadHotPage() {
        Map<String, String> map = new HashMap<String, String>();
        map.put("type", "2");
        String buffer = HttpUtils.doPostRequest(Constant.CONVERT_LIST_URL, map);
        List<Convert> list = JsonParse.parseConvertJson(buffer);
        Message msg = Message.obtain();
        msg.what = 2;
        msg.obj = list;
        handler.sendMessage(msg);
    }

    private void loadNewPage() {
        Map<String, String> map = new HashMap<String, String>();
        map.put("type", "1");
        String buffer = HttpUtils.doPostRequest(Constant.CONVERT_LIST_URL, map);
        List<Convert> list = JsonParse.parseConvertJson(buffer);
        Message msg = Message.obtain();
        msg.what = 1;
        msg.obj = list;
        handler.sendMessage(msg);
    }

    private ConvertAdapter newConvertAdapter;
    private ConvertAdapter hotConvertAdapter;
    private List<Convert> list1;
    private List<Convert> list2;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    list1 = (List<Convert>) msg.obj;
                    newConvertAdapter = new ConvertAdapter(list1, getActivity());
                    newPrizeListView.setAdapter(newConvertAdapter);
                    break;
                case 2:
                    list2 = (List<Convert>) msg.obj;
                    hotConvertAdapter = new ConvertAdapter(list2, getActivity());
                    hotPrizeListView.setAdapter(hotConvertAdapter);
                    break;

            }
        }
    };

    private void initView() {
        newPrizeBtn = (Button) view.findViewById(R.id.newest_prize_btn);
        hotPrizeBtn = (Button) view.findViewById(R.id.hotest_prize_btn);
        scoreViewPager = (ViewPager) view.findViewById(R.id.score_shop_viewpager);
        scoreViewPager.setCurrentItem(0);
        initViewPager();
    }

    private void initViewPager() {
        viewList = new ArrayList<View>();
        view1 = LayoutInflater.from(getActivity()).inflate(R.layout.convert_listview, null);
        view2 = LayoutInflater.from(getActivity()).inflate(R.layout.convert_listview, null);
        viewList.add(view1);
        viewList.add(view2);
        newPrizeListView = (ListView) view1.findViewById(R.id.convert_listview);
        hotPrizeListView = (ListView) view2.findViewById(R.id.convert_listview);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.newest_prize_btn:
                scoreViewPager.setCurrentItem(0);
                break;
            case R.id.hotest_prize_btn:
                scoreViewPager.setCurrentItem(1);
                break;
        }
    }
}
